package com.aspire.nm.component.miniServerDemo.clientTest;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import com.aspire.nm.component.util.HttpTookit;


public class ServerTest_expensive {

/**
 * 
serverPort=7070
workQueueSize=500
maximumPoolSize=1000
corePoolSize=1000    
 */
    private AtomicInteger count_504=new AtomicInteger(0);
    private AtomicInteger count_503=new AtomicInteger(0);
    private AtomicInteger count_other=new AtomicInteger(0);
    private AtomicInteger count_200=new AtomicInteger(0);
    private AtomicInteger count_clientTimeout=new AtomicInteger(0);
    private AtomicInteger count_clientConnException=new AtomicInteger(0);
    private AtomicInteger count=new AtomicInteger(0);
    private AtomicInteger sucessRespCount=new AtomicInteger(0);
    

    
    private static int threadNum = 4;
    private static int countPerThread = 1000;
    private static int speedPerThread = 4;
//    private String url = "http://"+Main.serverIp4Test+":7071/demo/expensive";
    private static String url = "http://www.baidu.com";
//    private String url = "http://192.168.6.67:8081/s?u=d&s=ASERVICE&ts=13401190417&p=1&ex=001&c=r1r2cc3&mid=12345";
    private static String sucessResp = "";
    
    
    
    public static void main(String [] args) throws InterruptedException, InstantiationException, IllegalAccessException{
        if(args.length == 5){
            threadNum = Integer.parseInt(args[0]);
            countPerThread = Integer.parseInt(args[1]);
            speedPerThread = Integer.parseInt(args[2]);
            url = args[3];
            sucessResp = args[4];
        }
        new ServerTest_expensive().test();
    }
    public void test() throws InterruptedException, InstantiationException, IllegalAccessException{
        
        
        
        for(int i = 0;i < threadNum;i++){
            new Thread(){
                private HttpTookit httpTookit = new HttpTookit();
//                private FlowControl flowControl = new FlowControl();
                
//                FlowControl flowControl = new FlowControl(speedPerThread,UNIT.SECOND,new InvokeFlow(){
//                    @Override
//                    public boolean invoke(Flowable flowable) {
//                        try{
//                            String resp = httpTookit.doGet(url, null);
//                            if(resp.trim().equals(sucessResp)){
//                                sucessRespCount.incrementAndGet();
//                            }
//                            count_200.incrementAndGet();
//                        }catch(RuntimeException e){
//                            if(e.getMessage().endsWith("504")){
//                                count_504.incrementAndGet();
//                            }else if(e.getMessage().endsWith("503")){
//                                count_503.incrementAndGet();
//                            }else{
//                                count_other.incrementAndGet();
//                            }
//                        }catch(java.net.SocketTimeoutException e1){
//                            //并发稿，server的workQueueSize值高，可能会导致socket无法在服务端得到及时处理，会引发client socketTimeOut
//                            count_clientTimeout.incrementAndGet();
//                        }catch(java.net.ConnectException e2){
//                          //并发稿，server的workQueueSize值低，
//                            count_clientConnException.incrementAndGet();
//                        }
//                        catch (Throwable e) {
//                            e.printStackTrace();
//                            count_other.incrementAndGet();
//                        } finally{
//                            count.incrementAndGet();
//                        }
//                        return true;
//                    }
//                });
                public void run(){
                    for(int j = 0 ;j < countPerThread ; j ++){
//                        flowControl.sleepForInvoke(new Flowable(){
//
//                            @Override
//                            public int flowTimes() {
//                                return 1;
//                            }
//
//                            @Override
//                            public Flowable[] splitForRemain(int times) {
//
//                                return null;
//                            }
//
//                        });
                    }
                    try {
                        httpTookit.release();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        
        int old_c = 0;
        while(true){
            if(count.get() == threadNum * countPerThread){
                
                System.out.println("total=" + threadNum * countPerThread + "("+sucessRespCount+")"+
                        " ,200=" + count_200+
                        " ,504=" + count_504+" ,503=" + count_503 +
                        " ,clientTimeOut="+count_clientTimeout +
                        " ,clientConnException="+count_clientConnException+ 
                        " ,other=" +count_other);
                

                break;
            }else{
                Thread.sleep(1000);
                int c = count.get();
                
                
                
                System.out.println("total=" + c + "("+sucessRespCount+")"+
                        " ,200=" + count_200+
                        " ,504=" + count_504+" ,503=" + count_503 +
                        " ,clientTimeOut="+count_clientTimeout +
                        " ,clientConnException="+count_clientConnException+ 
                        " ,other=" +count_other +" ,speed=" + (c-old_c) );
                old_c = c;
            }
        }
        
        
    }
    
}

